package middleware

import (
	"gitee.com/daofeixian/dfx_golang_toolset/common/res"
	"github.com/gin-gonic/gin"
)

func GetBind[T any](c *gin.Context) (data T) {
	// 从上下文中获取绑定的数据
	return c.MustGet("required").(T)
}

func BindJson[T any](c *gin.Context) {
	var data T
	err := c.BindJSON(&data)
	if err != nil {
		res.FailWithBindingError(err, c)
		//绑定失败，返回错误信息 c.Abort()的作用是中断后续处理
		c.Abort()
		return
	}

	c.Set("required", data)
}
